package com.android.controller;

/** this is test controller created 
 * By Pradnya to test the code
 */
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;

import android.graphics.Path;
import android.util.Log;
import android.view.MotionEvent;

import com.android.networking.connection.Connection;
import com.android.networking.message.ControlMessage;
import com.android.networking.message.DataMessage;

public class Controller {
	Connection conn;
	public boolean test(){
		try{
		//this code is for main server which will open server socket, and keep listening
        // on new client connection and maintain the connection pool. 
        Log.d("GuetchServer","Server started..");
		conn = Connection.getInstanceOfConnection();
		ServerSocket sock = conn.openServerSocket();
		Log.d("GuetchServer","Client Connected..");
		
		//conn.connectionWait();
		//Thread.sleep(50000);
		
		}catch(Exception ex){
			ex.printStackTrace();
			return false;
		}
		return true;
	}
	
	public void sendMessage(ArrayList<MotionEvent> eventArray){
	if(conn == null){
		conn = Connection.getInstanceOfConnection();
	}
	ArrayList<PrintWriter> out = conn.getClientOuputStreams();
		
		if(out!=null){
			Log.d("Draw","In send message");
			DataMessage d = new DataMessage(0, 1, eventArray, "", new ArrayList(), "",0,0,"");
			for(PrintWriter s : out){
				try{
					String str = d.toString();
					s.println(str);
					Log.d("Draw",str);
				}catch(Exception ex){
					ex.printStackTrace();
					System.exit(0);
				}
			}
		}
	}
	
	
	public void sendMessage(StringBuffer pathArray){
		ArrayList<PrintWriter> out = conn.getClientOuputStreams();
		
		if(out!=null){
			Log.d("Draw","In send message");
			//DataMessage d = new DataMessage(0, 1, pathArray, "", "", new ArrayList(), "");
			for(PrintWriter s : out){
				try{
					s.print(pathArray.toString());
					Log.d("Draw","sent message");
				}catch(Exception ex){
					ex.printStackTrace();
					System.exit(0);
				}
			}
		}
	}
}
